package com.ydm.myapp001.utils;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.widget.Toast;


import com.ydm.myapp001.R;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import woyou.aidlservice.jiuiv5.ICallback;
import woyou.aidlservice.jiuiv5.IWoyouService;


public class AidlUtil {
    private static final String SERVICE＿PACKAGE = "woyou.aidlservice.jiuiv5";
    private static final String SERVICE＿ACTION = "woyou.aidlservice.jiuiv5.IWoyouService";

    private IWoyouService woyouService;
    private static AidlUtil mAidlUtil = new AidlUtil();
    private Context context;

    private AidlUtil() {
    }

    public static AidlUtil getInstance() {
        return mAidlUtil;
    }

    /**
     * 连接服务
     *
     * @param context context
     */
    public void connectPrinterService(Context context) {
        this.context = context.getApplicationContext();
        Intent intent = new Intent();
        intent.setPackage(SERVICE＿PACKAGE);
        intent.setAction(SERVICE＿ACTION);
        context.getApplicationContext().startService(intent);
        context.getApplicationContext().bindService(intent, connService, Context.BIND_AUTO_CREATE);
    }

    /**
     * 断开服务
     *
     * @param context context
     */
    public void disconnectPrinterService(Context context) {
        if (woyouService != null) {
            context.getApplicationContext().unbindService(connService);
            woyouService = null;
        }
    }

    public boolean isConnect() {
        return woyouService != null;
    }

    private ServiceConnection connService = new ServiceConnection() {

        @Override
        public void onServiceDisconnected(ComponentName name) {
            woyouService = null;
        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            woyouService = IWoyouService.Stub.asInterface(service);
        }
    };

    public ICallback generateCB(final PrinterCallback printerCallback){
        return new ICallback.Stub(){


            @Override
            public void onRunResult(boolean isSuccess) throws RemoteException {

            }

            @Override
            public void onReturnString(String result) throws RemoteException {
                printerCallback.onReturnString(result);
            }

            @Override
            public void onRaiseException(int code, String msg) throws RemoteException {

            }
        };
    }

    /**
     * 设置打印浓度
     */
    private int[] darkness = new int[]{0x0600, 0x0500, 0x0400, 0x0300, 0x0200, 0x0100, 0,
            0xffff, 0xfeff, 0xfdff, 0xfcff, 0xfbff, 0xfaff};

    public void setDarkness(int index) {
        if (woyouService == null) {
            Toast.makeText(context, R.string.toast_2,Toast.LENGTH_LONG).show();
            return;
        }

        int k = darkness[index];
        try {
            woyouService.sendRAWData(ESCUtil.setPrinterDarkness(k), null);
            woyouService.printerSelfChecking(null);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    /**
     * 取得打印机系统信息，放在list中
     *
     * @return list
     */
    public List<String> getPrinterInfo(PrinterCallback printerCallback) {
        if (woyouService == null) {
            return null;
        }

        List<String> info = new ArrayList<>();
        try {
            woyouService.getPrintedLength(generateCB(printerCallback));
            info.add(woyouService.getPrinterSerialNo());
            info.add(woyouService.getPrinterModal());
            info.add(woyouService.getPrinterVersion());
            info.add(printerCallback.getResult());
            info.add("");
            //info.add(woyouService.getServiceVersion());
            PackageManager packageManager = context.getPackageManager();
            try {
                PackageInfo packageInfo = packageManager.getPackageInfo(SERVICE＿PACKAGE, 0);
                if(packageInfo != null){
                    info.add(packageInfo.versionName);
                    info.add(packageInfo.versionCode+"");
                }else{
                    info.add("");info.add("");
                }
            } catch (PackageManager.NameNotFoundException e) {
                e.printStackTrace();
            }

        } catch (RemoteException e) {
            e.printStackTrace();
        }
        return info;
    }

    /**
     * 初始化打印机
     */
    public void initPrinter() {
        if (woyouService == null) {
            return;
        }

        try {
            woyouService.printerInit(null);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }





    /**
     * 打印文字
     */
    public void printText() {
        if (woyouService == null) {
            return;
        }

        try {
            /*示例一：
            if (isBold) {
                woyouService.sendRAWData(ESCUtil.boldOn(), null);
            } else {
                woyouService.sendRAWData(ESCUtil.boldOff(), null);
            }

            if (isUnderLine) {
                woyouService.sendRAWData(ESCUtil.underlineWithOneDotWidthOn(), null);
            } else {
                woyouService.sendRAWData(ESCUtil.underlineOff(), null);
            }

            woyouService.printTextWithFont(content, null, size, null);
            woyouService.lineWrap(3, null);
            */


            /*示例二：
            woyouService.sendRAWData(ESCUtil.boldOn(), null);
            woyouService.setAlignment(1, null);
            woyouService.printTextWithFont("聚梦公司", null, 48, null);
            woyouService.lineWrap(1, null);
            woyouService.sendRAWData(ESCUtil.boldOff(), null);
            woyouService.setAlignment(0, null);
            woyouService.printTextWithFont("2010年3月份上市", null, 24, null);
            woyouService.lineWrap(2, null);
            woyouService.printTextWithFont("公司简介：", null, 24, null);
            woyouService.lineWrap(1, null);
            woyouService.printTextWithFont("聚梦是中国最大的软件科技公司，具有全中国最顶尖的开发团队，进入聚梦是全球每个软件开发人员的梦想！", null, 24, null);
            woyouService.lineWrap(2, null);
            woyouService.sendRAWData(ESCUtil.boldOn(), null);
            woyouService.setAlignment(2, null);
            byte[] sperateLine = new byte[]{0x2D,0x2D,0x2D,0x2D,0x2D,0x2D,0x2D, 0x2D,0x2D,0x2D,0x2D,0x2D,0x2D, 0x2D,0x2D,0x2D,0x2D,0x2D,0x2D, 0x2D,0x2D,0x2D,0x2D,0x2D,0x2D,0x2D,0x2D, 0x2D,0x2D,0x2D,0x2D,0x2D};
            woyouService.sendRAWData(sperateLine, null);
            woyouService.printTextWithFont("作者:    董鹏飞", null, 24, null);
            woyouService.lineWrap(4, null);
            */



            /*示例三:
            woyouService.sendRAWData(ESCUtil.boldOn(), null);
            woyouService.setAlignment(1, null);
            woyouService.printTextWithFont("长江路店", null, 28, null);
            woyouService.lineWrap(1, null);
            woyouService.sendRAWData(ESCUtil.boldOff(), null);
            woyouService.printTextWithFont("请保留本票作为退货依据", null, 28, null);
            woyouService.lineWrap(1, null);
            woyouService.printTextWithFont("凭此单至服务台开发票", null, 28, null);
            woyouService.lineWrap(1, null);
            byte[] sperateLine = new byte[]{0x2D,0x2D,0x2D,0x2D,0x2D,0x2D,0x2D, 0x2D,0x2D,0x2D,0x2D,0x2D,0x2D, 0x2D,0x2D,0x2D,0x2D,0x2D,0x2D, 0x2D,0x2D,0x2D,0x2D,0x2D,0x2D,0x2D,0x2D, 0x2D,0x2D,0x2D,0x2D,0x2D};
            woyouService.setFontSize(24, null);
            woyouService.sendRAWData(sperateLine, null);
            woyouService.lineWrap(1, null);
            woyouService.setAlignment(0, null);
            woyouService.printTextWithFont("商品   价格   数量  金额  节省", null, 24, null);
            woyouService.lineWrap(1, null);
            woyouService.printTextWithFont("喜之郎果冻双水蜜饯", null, 24, null);
            woyouService.lineWrap(1, null);
            woyouService.printTextWithFont("11011  3.3    2      6.6   0.7-", null, 24, null);
            woyouService.lineWrap(1, null);
            woyouService.printTextWithFont("上好佳鲜虾条", null, 24, null);
            woyouService.lineWrap(1, null);
            woyouService.printTextWithFont("11011  5.8    1      5.8   0.1-", null, 24, null);
            woyouService.lineWrap(1, null);
            woyouService.printTextWithFont("漂亮保鲜膜", null, 24, null);
            woyouService.lineWrap(1, null);
            woyouService.printTextWithFont("11011  6.3    2      12.6  0.7-", null, 24, null);
            woyouService.lineWrap(1, null);
            woyouService.sendRAWData(sperateLine, null);
            woyouService.lineWrap(1, null);
            woyouService.printTextWithFont("内场满50赠送30元代金券1张", null, 28, null);
            woyouService.lineWrap(1, null);
            woyouService.setFontSize(24, null);
            woyouService.sendRAWData(sperateLine, null);
            woyouService.lineWrap(1, null);
            woyouService.printTextWithFont("总 金 额:                  50.70", null, 24, null);
            woyouService.lineWrap(1, null);
            woyouService.printTextWithFont("应付金额:                  48.60", null, 24, null);
            woyouService.lineWrap(1, null);
            woyouService.printTextWithFont("优惠金额:                   2.10", null, 24, null);
            woyouService.lineWrap(1, null);
            woyouService.printTextWithFont("顾客付款:                  48.60", null, 24, null);
            woyouService.lineWrap(1, null);
            woyouService.printTextWithFont("找    零:                   0.00", null, 24, null);
            woyouService.lineWrap(1, null);
            woyouService.printTextWithFont("卡号: 61367546    积分:866", null, 24, null);
            woyouService.lineWrap(1, null);
            woyouService.sendRAWData(sperateLine, null);
            woyouService.lineWrap(1, null);
            woyouService.printTextWithFont("消费满50元凭小票机动车可免费停车2小时", null, 28, null);
            woyouService.lineWrap(4, null);
            */


            //示例4:
            woyouService.sendRAWData(ESCUtil.boldOn(), null);
            woyouService.setAlignment(1, null);
            woyouService.printTextWithFont("长江路店", null, 28, null);
            woyouService.lineWrap(1, null);
            woyouService.sendRAWData(ESCUtil.boldOff(), null);
            woyouService.printTextWithFont("请保留本票作为退货依据", null, 28, null);
            woyouService.lineWrap(1, null);
            woyouService.printTextWithFont("凭此单至服务台开发票", null, 28, null);
            woyouService.lineWrap(1, null);
            woyouService.printTextWithFont("--------------------------------", null, 24, null);
            woyouService.lineWrap(1, null);
            woyouService.setAlignment(0, null);
            woyouService.printTextWithFont("商品   价格   数量  金额  节省", null, 24, null);
            woyouService.lineWrap(1, null);
            woyouService.printTextWithFont("喜之郎果冻双水蜜饯", null, 24, null);
            woyouService.lineWrap(1, null);
            woyouService.printTextWithFont("11011  3.3    2      6.6   0.7-", null, 24, null);
            woyouService.lineWrap(1, null);
            woyouService.printTextWithFont("上好佳鲜虾条", null, 24, null);
            woyouService.lineWrap(1, null);
            woyouService.printTextWithFont("11011  5.8    1      5.8   0.1-", null, 24, null);
            woyouService.lineWrap(1, null);
            woyouService.printTextWithFont("漂亮保鲜膜", null, 24, null);
            woyouService.lineWrap(1, null);
            woyouService.printTextWithFont("11011  6.3    2      12.6  0.7-", null, 24, null);
            woyouService.lineWrap(1, null);
            woyouService.printTextWithFont("--------------------------------", null, 24, null);
            woyouService.lineWrap(1, null);
            woyouService.printTextWithFont("内场满50赠送30元代金券1张", null, 28, null);
            woyouService.lineWrap(1, null);
            woyouService.printTextWithFont("--------------------------------", null, 24, null);
            woyouService.lineWrap(1, null);
            woyouService.printTextWithFont("总 金 额:                  50.70", null, 24, null);
            woyouService.lineWrap(1, null);
            woyouService.printTextWithFont("应付金额:                  48.60", null, 24, null);
            woyouService.lineWrap(1, null);
            woyouService.printTextWithFont("优惠金额:                   2.10", null, 24, null);
            woyouService.lineWrap(1, null);
            woyouService.printTextWithFont("顾客付款:                  48.60", null, 24, null);
            woyouService.lineWrap(1, null);
            woyouService.printTextWithFont("找    零:                   0.00", null, 24, null);
            woyouService.lineWrap(1, null);
            woyouService.printTextWithFont("卡号: 61367546    积分:866", null, 24, null);
            woyouService.lineWrap(1, null);
            woyouService.printTextWithFont("--------------------------------", null, 24, null);
            woyouService.lineWrap(1, null);
            woyouService.printTextWithFont("消费满50元凭小票机动车可免费停车2小时", null, 28, null);
            woyouService.lineWrap(4, null);
        } catch (RemoteException e) {
            e.printStackTrace();
        }

    }
}
